package com.mcc.system.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.system.domain.SysNotice;
import com.mcc.system.mapper.SysNoticeMapper;
import com.mcc.system.service.ISysNoticeService;

/**
 * 公告 服务层实现
 *
 * @author gxy
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {
    @Autowired
    private SysNoticeMapper noticeMapper;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.selectById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(notice)) {
            queryWrapper.like(ObjectUtil.isNotEmpty(notice.getNoticeTitle()), "notice_title", notice.getNoticeTitle());
            queryWrapper.eq(ObjectUtil.isNotEmpty(notice.getNoticeType()), "notice_type", notice.getNoticeType());
            queryWrapper.eq(ObjectUtil.isNotEmpty(notice.getStatus()), "status", notice.getStatus());
            queryWrapper.like(ObjectUtil.isNotEmpty(notice.getCreateBy()), "create_by", notice.getCreateBy());
        }
        queryWrapper.orderByDesc("create_time");
        return noticeMapper.selectList(queryWrapper);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        notice.setCreateBy(SecurityUtils.getUsername());
        notice.setCreateTime(new Date());
        return noticeMapper.insert(notice);
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        notice.setUpdateBy(SecurityUtils.getUsername());
        notice.setUpdateTime(new Date());
        return noticeMapper.updateById(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return noticeMapper.deleteById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        if (noticeIds.length == 0) return 0;
        return noticeMapper.deleteBatchIds(ListUtil.toList(noticeIds));
    }
}
